package action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import proxy.ProxyFactory;
import service.BoxAndPackageService;
import service.BoxAndPackageServiceInterface;
import utils.CalculateDate;
import utils.PageBean;

import beans.Agent;
import beans.Branch;
import beans.Goods;
import beans.LogAbnormalBox;
import beans.LogAbnormalParcel;
import beans.Package;
import beans.SmsStatus;
import beans.Terminal;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dao.SmsStatusDao;

@SuppressWarnings("serial")
public class BoxAndPackageAction extends ActionSupport {

	private int branchID;
	private int terminalID;
	private int packageID;
	private int goodsID;
	private int boxNum;
	private int type;
	private int day;
	private int problemID;
	private int status;
	private int operate;// 包裹操作
	private int detained;// 滞留等级
	private int curpage;// 当前页
	private int pageSize;// 页大小
	private String newPhone;// 新手机号码
	private String flag;
	private String reason;// 未收到短信原因
	private String comment;

	private Package p = null;
	private LogAbnormalBox logAbnormalBox = null;
	private LogAbnormalParcel logAbnormalParcel = null;

	List<Package> packages;
	List<Goods> goodsList = null;
	List<Branch> branchs = null;
	List<Terminal> terminals = null;
	List<LogAbnormalBox> logAbnormalBoxs = null;
	List<LogAbnormalParcel> logAbnormalParcels = null;
	PageBean<Package> unTakenParcel = null;

	private SmsStatusDao smsStatusDao = new SmsStatusDao();

	private BoxAndPackageServiceInterface boxPkgService = (BoxAndPackageServiceInterface) ProxyFactory
			.getProxyInstance(new BoxAndPackageService());

	ActionContext actionContext = ActionContext.getContext();
	Map<String, Object> session = actionContext.getSession();
	Agent agent = (Agent) session.get("admin");

	// PackageDao packageDao = new PackageDao();

	/**
	 * 包裹异常信息详情
	 * 
	 * @return
	 */
	public String getAbnormalDetails() {
		logAbnormalBoxs = boxPkgService.findAbnormalBox(terminalID, boxNum);
		logAbnormalParcels = boxPkgService.findAbnormalParcel(terminalID, boxNum);
		return "abnormalDetails";
	}

	public String dealPackage() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Agent admin = (Agent) session.get("admin");
		packages = boxPkgService.dealPackage(packages, p, packageID, admin);
		// p = packageDao.getPackageDetailsByID(packageID);
		// packages = packageDao.dealPackage(packageID);
		// boxDao.changeBoxStatus(p.getTerminal().getTerminalId(), p.getBoxId(),
		// 0, 0);
		return "success";
	}

	public String taken() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Agent admin = (Agent) session.get("admin");
		if (packageID != 0) {
			boxPkgService.take(terminalID, boxNum, type, packageID, admin);
		}
		if (goodsID != 0) {
			boxPkgService.takeGoods(terminalID, boxNum, type, goodsID, admin);
		}
		if (packageID == 0 && goodsID == 0) {
			boxPkgService.clearBox(terminalID, boxNum);
		}
		// boxDao.setBoxAvailable(terminalID, boxNum, type);
		// packageDao.setPackageTaken(terminalID, boxNum);
		return "success";

	}

	public String back() {
		boxPkgService.back(terminalID, boxNum, type);
		// boxDao.setBoxAvailable(terminalID, boxNum, type);
		// packageDao.setPackageBack(terminalID, boxNum);
		return "success";
	}

	public String unTaken() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Agent admin = (Agent) session.get("admin");
		if (packageID == 1) {
			boxPkgService.unTaken(terminalID, boxNum, type, admin);
		}
		if (goodsID == 1) {
			boxPkgService.unTakenGoods(terminalID, boxNum, type);
		}
		// boxDao.setBoxOccupation(terminalID, boxNum);
		// packageDao.setPackageUnTaken(terminalID, boxNum);
		return "success";
	}

	/**
	 * 查询箱格存物详情
	 * 
	 * @return
	 */
	public String getBoxStoreDetails() {
		packages = boxPkgService.getStorePackage(terminalID, boxNum);
		goodsList = boxPkgService.getStoreGoods(terminalID, boxNum);
		if (!packages.isEmpty()) {
			return "success";
		} else if (!goodsList.isEmpty()) {
			return "goodsList";
		} else {
			return "error";
		}
	}

	/**
	 * 根据设备网点获取设备
	 * 
	 * @return
	 */
	public String getTerminalByBranch() {
		terminals = boxPkgService.getTerminalByBranch(branchID);
		return "selectTerminal";
	}

	/**
	 * 返回滞留包裹查询结果
	 * 
	 * @return
	 */
	public String getUnTakenPakcages() {
		if (pageSize == 0) {
			pageSize = 10;
		}
		if (curpage == 0) {
			curpage = 1;
		}
		unTakenParcel = boxPkgService.getUnTakenPakcages(curpage, pageSize,
				agent.getId(), branchID, terminalID, day);
		if (unTakenParcel.getList().size() <= 0) { // 判断当前页有没数据
			while (curpage > 1) {
				curpage--;
				unTakenParcel = boxPkgService.getUnTakenPakcages(curpage,
						pageSize, agent.getId(), branchID, terminalID, day);
				if (unTakenParcel.getList().size() > 0) {
					break;
				}
			}
		}
		packages = unTakenParcel.getList();
		Date enddate = new Date();
		for (Package p : packages) {
			p.leftTime = String.valueOf(CalculateDate.getLeftTime(new Date(p
					.getStoreTime().getTime()), enddate));
			List<SmsStatus> list = smsStatusDao.getByParcelID(p);
			if (list.size() != 0) {
				p.msgStatus = list.get(0).getStatus();
			} else {
				p.msgStatus = 0;
			}
		}
		if ("list".equals(flag)) {
			return "unTaken";
		} else {
			return "unTakenAjax";
		}
	}

	/**
	 * 返回相应的JSP页面
	 * 
	 * @return
	 */
	public String returnJSP() {
		if ("sign".equals(flag)) {
			return "sign";
		} else if ("jsp".equals(flag)) {
			return "jsp";
		} else {
			return "error";
		}

	}

	/**
	 * 根据代理商获取设备网点
	 * 
	 * @return
	 */
	public String getBranchByAgent() {
		branchs = boxPkgService.getBranchByAgent(agent.getId());
		return "selectBranch";
	}

	/**
	 * 解锁包裹
	 */
	public void unLockParcel() {
		boxPkgService.unLockParcel(p, packageID, agent);
	}

	public void signParcel() {
		boxPkgService.signParcel(newPhone, p, packageID, agent, problemID,
				operate, detained, reason, comment);
	}

	public int getBoxNum() {
		return boxNum;
	}

	public void setBoxNum(int boxNum) {
		this.boxNum = boxNum;
	}

	public int getTerminalID() {
		return terminalID;
	}

	public void setTerminalID(int terminalID) {
		this.terminalID = terminalID;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getPackageID() {
		return packageID;
	}

	public void setPackageID(int packageID) {
		this.packageID = packageID;
	}

	public Package getP() {
		return p;
	}

	public void setP(Package p) {
		this.p = p;
	}

	public List<Package> getPackages() {
		return packages;
	}

	public void setPackages(List<Package> packages) {
		this.packages = packages;
	}

	public int getGoodsID() {
		return goodsID;
	}

	public void setGoodsID(int goodsID) {
		this.goodsID = goodsID;
	}

	public List<Goods> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<Goods> goodsList) {
		this.goodsList = goodsList;
	}

	public int getBranchID() {
		return branchID;
	}

	public void setBranchID(int branchID) {
		this.branchID = branchID;
	}

	public List<Branch> getBranchs() {
		return branchs;
	}

	public void setBranchs(List<Branch> branchs) {
		this.branchs = branchs;
	}

	public List<Terminal> getTerminals() {
		return terminals;
	}

	public void setTerminals(List<Terminal> terminals) {
		this.terminals = terminals;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public int getProblemID() {
		return problemID;
	}

	public void setProblemID(int problemID) {
		this.problemID = problemID;
	}

	public int getOperate() {
		return operate;
	}

	public void setOperate(int operate) {
		this.operate = operate;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public int getDetained() {
		return detained;
	}

	public void setDetained(int detained) {
		this.detained = detained;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public int getCurpage() {
		return curpage;
	}

	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageBean<Package> getUnTakenParcel() {
		return unTakenParcel;
	}

	public void setUnTakenParcel(PageBean<Package> unTakenParcel) {
		this.unTakenParcel = unTakenParcel;
	}

	public String getNewPhone() {
		return newPhone;
	}

	public void setNewPhone(String newPhone) {
		this.newPhone = newPhone;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public LogAbnormalBox getLogAbnormalBox() {
		return logAbnormalBox;
	}

	public void setLogAbnormalBox(LogAbnormalBox logAbnormalBox) {
		this.logAbnormalBox = logAbnormalBox;
	}

	public LogAbnormalParcel getLogAbnormalParcel() {
		return logAbnormalParcel;
	}

	public void setLogAbnormalParcel(LogAbnormalParcel logAbnormalParcel) {
		this.logAbnormalParcel = logAbnormalParcel;
	}

	public List<LogAbnormalBox> getLogAbnormalBoxs() {
		return logAbnormalBoxs;
	}

	public void setLogAbnormalBoxs(List<LogAbnormalBox> logAbnormalBoxs) {
		this.logAbnormalBoxs = logAbnormalBoxs;
	}

	public List<LogAbnormalParcel> getLogAbnormalParcels() {
		return logAbnormalParcels;
	}

	public void setLogAbnormalParcels(List<LogAbnormalParcel> logAbnormalParcels) {
		this.logAbnormalParcels = logAbnormalParcels;
	}

}
